<template>
  <div>
    <van-address-list
      v-model="chosenAddressId"
      :list="list"
      add-button-text="+新建收货地址"
      default-tag-text="默认"
      @add="onAdd"
      @edit="onEdit"
      @select="select"
    />
  </div>
</template>

<script>
import { Toast } from "vant";
import { reqSitelist } from "../../http/http";
export default {
  data() {
    return {
      chosenAddressId: "1",
      list: [],
    };
  },
  methods: {
    onAdd() {
      Toast("新增地址");
      this.$router.push("/addsite?site=1");
    },
    onEdit(item, index) {
      console.log(item);
      Toast("编辑地址:" + item.name);
      this.$router.push('/addsite?id='+item.id+'&site=2');
    },
    select(item) {
      this.$router.push('/order?site='+item.id)
    },
  },
  mounted() {
    reqSitelist({
      uid: JSON.parse(localStorage.getItem("user")).uid,
    }).then((res) => {
      if (res.data.code == 200) {
        this.list = res.data.list ? res.data.list : [];
        this.list = this.list.map((item) => {
          return {
            id: item.id,
            name: item.username,
            tel: item.userphone,
            address: item.location + item.useraddress,
            isDefault: item.status == 1 ? true : false,
          };
        });
        console.log(this.list);
      }
    });
  },
};
</script>

<style></style>
